package com.nurt.controller.order;

import com.nurt.service.OrderService;
import com.nurt.service.impl.OrderServiceImpl;
import com.nurt.utils.ReturnCode;
import com.nurt.utils.ServletHelper;
import com.alibaba.fastjson2.JSONObject;
import com.nurt.dto.ResultDto;
import com.nurt.entity.OrderDishEntity;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = {"/orderDishes/*"})
public class OrderDishController extends HttpServlet{
    private OrderService orderService;

    public void init() {
        orderService = new OrderServiceImpl();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String orderId = pathParts[1];

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        try {
            List<OrderDishEntity> orderDishes = orderService.getCartContents(Integer.parseInt(orderId));
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().write(ResultDto.success(orderDishes));
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            response.getWriter().write(ResultDto.fail(ReturnCode.INTERNAL_SERVER_ERROR.getCode(), e.getMessage()));
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String orderId = pathParts[1];
        
        JSONObject jsonObject = ServletHelper.getRequestBodyAsJson(request);
        int dishId = jsonObject.getIntValue("dishId");
        
        int orderDishId = orderService.addToCart(Integer.parseInt(orderId), dishId);
        OrderDishEntity orderDish = new OrderDishEntity();
        orderDish.setOrderDishId(orderDishId);
        orderDish.setOrderId(Integer.parseInt(orderId));
        orderDish.setDishId(dishId);
        
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(ResultDto.success(orderDish));

    }

    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String orderId = pathParts[1];
        String dishId = pathParts[3];

        JSONObject jsonObject = ServletHelper.getRequestBodyAsJson(request);
        int quantity = jsonObject.getIntValue("quantity");

        orderService.updateCart(Integer.parseInt(orderId), Integer.parseInt(dishId), quantity);

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(ResultDto.success(null));
    }

    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pathInfo = request.getPathInfo();
        String[] pathParts = pathInfo.split("/");
        String orderId = pathParts[1];
        String dishId = pathParts[3];

        orderService.removeFromCart(Integer.parseInt(orderId), Integer.parseInt(dishId));

        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(ResultDto.success(null));
    }
}
